package com.nblog.controller.admin;
import com.jfinal.aop.Before;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.nblog.annotation.ControllerBind;
import com.nblog.auth.annotation.RequiresPermissions;
import com.nblog.interceptor.admin.LoginInterceptor;
import com.nblog.interceptor.admin.SysLogInterceptor;
import com.nblog.model.SysUser;
import com.nblog.plugin.ioc.annotation.JFAutowired;
import com.nblog.service.SysRoleService;
import com.nblog.service.SysUserService;
import com.nblog.utils.CommonUtils;
import com.nblog.view.InvokeResult;
@ControllerBind(controllerKey="/admin/user")
@Before({LoginInterceptor.class,SysLogInterceptor.class})
public class UserController extends BaseControlller {
	@JFAutowired
	SysUserService sysUserService;
	@JFAutowired
	SysRoleService sysRoleService;
	@RequiresPermissions(value={"/admin/user"})
	public void index(){
		render("index.html");
	}
	@RequiresPermissions(value={"/admin/user"})
	public void getData(){
		int page=getParaToInt("page", 1);
		int rows=getParaToInt("rows", 20);
		String keyword=getPara("keyword");
		Page<SysUser> sysUserPage=sysUserService.getSysUserPage(page, rows, keyword);
		renderJson(CommonUtils.pageToDataGrid(sysUserPage));
	}
	/**
	 * 分配权限
	 */
	@RequiresPermissions(value={"/admin/user"})
	public void userRoleSetting() {
		Integer uid=this.getParaToInt("uid");
		this.setAttr("item", SysUser.dao.findById(uid));
		InvokeResult result=sysRoleService.getRoleZtreeViewList(uid);
		this.setAttr("jsonTree", result);
		render("user_role_setting.html");
	}
	@Before(Tx.class)
	@RequiresPermissions(value={"/admin/user"})
	public void saveUserRoles(){
		Integer uid=this.getParaToInt("id");
		String roleIds=this.getPara("roleIds");
		InvokeResult result=sysUserService.changeUserRoles(uid, roleIds);
		this.renderJson(result);
	}
	@RequiresPermissions(value={"/admin/user"})
	public void edit(){
		render("edit.html");
	}
	@RequiresPermissions(value={"/admin/user"})
	public void saveUser(){
		String username=this.getPara("name");
		String password=this.getPara("password");
		String phone=this.getPara("phone");
		String email=this.getPara("email");
		Integer id=this.getParaToInt("id");
		String des=this.getPara("des");
		InvokeResult result=sysUserService.save(id, username, password, des,phone, email);
		this.renderJson(result); 
	}
	@RequiresPermissions(value={"/admin/user"})
	public void setVisible(){
		Integer visible=this.getParaToInt("visible");
		String ids=this.getPara("ids");
		InvokeResult result=sysUserService.setVisible(ids, visible);
		this.renderJson(result);
	}
	@RequiresPermissions(value={"/admin/user"})
	@Before({POST.class,Tx.class})
	public void del(){
		String idStrs=this.getPara("ids");
		this.renderJson(sysUserService.del(idStrs));
	}
}
